package com.csdj.server.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csdj.server.mapper.DepartmentMapper;
import com.csdj.server.pojo.Department;
import com.csdj.server.pojo.RespBean;
import com.csdj.server.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ZouLi
 * @since 2022-12-12
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {

    /**
     * 注入 DepartmentMapper
     **/
    @Autowired(required = false)
    private DepartmentMapper departmentMapper;

    /**
     * @Description: 根据父级id获取子级部门
     * @Param [pid]
     * @Return java.util.List<com.csdj.server.pojo.Department>
     * @Throws
     **/
    @Override
    public List<Department> getAllDepartmentById(Integer pid) {
        return departmentMapper.getAllDepartmentById(pid);
    }

    @Override
    public RespBean addDepartment(Department department) {
        department.setEnabled(true);
        // 调用存储过程 会将存储过程的返回值再次添加到实体类参数
        departmentMapper.addDep(department);
        if (department.getResult() == 1){
            return RespBean.success("添加成功！");
        }
        return RespBean.error("添加失败！");
    }

    @Override
    public RespBean delDepartment(Integer id) {
        Department department = new Department();
        department.setId(id);
        departmentMapper.delDep(department);
        if (department.getResult() == -2){
            return RespBean.error("该部门下有子部门, 删除失败！");
        }else if (department.getResult() == -1){
            return RespBean.error("该部门下有员工, 删除失败！");
        }else{
            return RespBean.success("删除成功！");
        }
    }


}
